package org.ran.demo.http.end;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 以固定字符串结尾
 * @author caoran
 *
 */
public class EndWithChar {
    private static final String END = "asd";
    public static void main(String[] args) throws Exception{
        new Thread(new Server()).start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(new Client()).start();
    }
    
    private static class Server implements Runnable {

        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket();
                serverSocket.bind(new InetSocketAddress("127.0.0.1", 8080));
                Socket socket = serverSocket.accept();
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                List<Byte> bytes = new ArrayList<>();
                int b;
                while ((b=in.read())>-1) {
                    bytes.add((byte)b);
                    if (isEnd(bytes)) {
                        break;
                    }
                }
                byte[] res = new byte[bytes.size()];
                for (int i = 0; i < bytes.size(); i++) {
                    res[i] = bytes.get(i);
                }
                String text = new String(res, StandardCharsets.ISO_8859_1);
                System.out.println("服务器收到请求："+text.substring(0,text.length()-END.length()));
                out.write(res);
                socket.close();
                serverSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    private static class Client implements Runnable{

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("127.0.0.1", 8080));
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                String request = "hello world";
                out.write((request+END).getBytes(StandardCharsets.ISO_8859_1));
                List<Byte> bytes = new ArrayList<>();
                int b;
                while ((b=in.read())>-1) {
                    bytes.add((byte)b);
                    if (isEnd(bytes)) {
                        break;
                    }
                }
                byte[] res = new byte[bytes.size()];
                for (int i = 0; i < bytes.size(); i++) {
                    res[i] = bytes.get(i);
                }
                String text = new String(res, StandardCharsets.ISO_8859_1);
                System.out.println("客户端收到响应："+text.substring(0,text.length()-END.length()));
                socket.close();
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    private static boolean isEnd(List<Byte> bytes) {
        if (bytes.size()<END.length()) {
            return false;
        }
        int length = bytes.size();
        int endLength = END.length();
        for (int i = 0; i < endLength; i++) {
            if (bytes.get(length-i-1)!=END.charAt(endLength-i-1)) {
                return false;
            }
        }
        return true;
    }
}
